package kumi.controller;

import com.alibaba.fastjson.JSON;
import kumi.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import static kumi.kumi.db.getConnection.getConnection;

/**
 * Created by Administrator on 2016/8/5 0005.
 */
@Controller
@RequestMapping(value = "/user")
public class Aoo {
    @RequestMapping(value="/login.do")
    @ResponseBody
    public String login(String username,String password,String phonenumber){
        User user=new User(username,password,phonenumber);
        insert(user);
        return JSON.toJSONString(user);
    }

    public static void insert(User user){
        //先得到数据库连接
        Connection connection = getConnection();

        //sql
        String sql ="INSERT INTO myusers(username,password,phonenumber) VALUES (?,?,?)";
        PreparedStatement preparedStatement =  null;

        try {

            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getUsername());
            preparedStatement.setString(2,user.getPassword());
            preparedStatement.setString(3,user.getPhonenumber());
            preparedStatement.execute();
            System.out.printf("insert successfully");
            preparedStatement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}


